//
// Created by mgts on 2016/7/17.
//

#include "ScanColor.h"
#include "ColorObtain.h"

#include <jni.h>
#include <android/log.h>

//ScanColor scanColor;

//JNIEXPORT void JNICALL Java_com_example_mgts_jni_ScanColorJNI_Init
//(JNIEnv * env, jobject obj, jint width, jint height, jobject javaObject)
//{
 //   scanColor.env = env;
   // scanColor.jobj = env->NewGlobalRef(obj);;
   // int retGvm = env->GetJavaVM(&scanColor.javaVM);

   // scanColor.javaObject = env->NewGlobalRef(javaObject);

   // scanColor.Init(width, height);
// }

// JNIEXPORT void JNICALL Java_com_example_mgts_jni_ScanColorJNI_UpdatBuff
//(JNIEnv * env, jobject obj, jbyteArray buff, jint width, jint height, jint bit, jint lenght)
//{
//LOGI("Java_com_example_mgts_jni_CppLink_UpdatCamera %d, %d, %d  ", width, height, bit);
//    jbyte * bytes = env->GetByteArrayElements (buff, 0);
//    scanColor.UpdatBuff((unsigned char *)bytes, width, height, bit);
//}

void ScanColor::Init(int width, int height,unsigned char * color)
{
    // rgbBuff = new unsigned char[width * height * 3];//最后乘以5是有5页
   // memset(color, 0, width * height * 3);
}

void ScanColor::UpDataColorEdit()
{
    JNIEnv *env;
    javaVM->AttachCurrentThread((JNIEnv **)&env, NULL);
//    jclass cls = env->GetObjectClass(jobj);

    jclass ColorEditClass = env->FindClass("com/example/mgts/jni/ColorEdit");

    jfieldID lightLevelID = env->GetFieldID( ColorEditClass, "lightLevel", "I");
    env->SetIntField( javaObject, lightLevelID, colorEdit.lightLevel);

    jfieldID ColorBlockID = env->GetFieldID(ColorEditClass, "colorBlock", "[Lcom/example/mgts/jni/ColorBlock;");
    jobjectArray colorBlockArray = (jobjectArray)env->GetObjectField(javaObject, ColorBlockID);

    int size = env->GetArrayLength(colorBlockArray);
    //LOGI("%d  ", size);
    for(int i = 0; i < size; i++)
    {
        jobject colorBlock = (jobject)env->GetObjectArrayElement(colorBlockArray, i);
        jclass ColorBlockClass = env->GetObjectClass(colorBlock);

        jfieldID lightLevelID = env->GetFieldID( ColorBlockClass, "lightLevel", "I");
        env->SetIntField( colorBlock, lightLevelID, colorEdit.colorBlock[i].lightLevel);

        jfieldID ratioID = env->GetFieldID( ColorBlockClass, "ratio", "F");
        env->SetFloatField( colorBlock, ratioID, colorEdit.colorBlock[i].ratio);

        jfieldID IDID = env->GetFieldID( ColorBlockClass, "ID", "I");
        env->SetIntField( colorBlock, IDID, colorEdit.colorBlock[i].ID);

        jfieldID minRID = env->GetFieldID( ColorBlockClass, "minR", "I");
        env->SetIntField( colorBlock, minRID, colorEdit.colorBlock[i].minR);

        jfieldID minGID = env->GetFieldID( ColorBlockClass, "minG", "I");
        env->SetIntField( colorBlock, minGID, colorEdit.colorBlock[i].minG);

        jfieldID minBID = env->GetFieldID( ColorBlockClass, "minB", "I");
        env->SetIntField( colorBlock, minBID, colorEdit.colorBlock[i].minB);

        jfieldID maxRID = env->GetFieldID( ColorBlockClass, "maxR", "I");
        env->SetIntField( colorBlock, maxRID, colorEdit.colorBlock[i].maxR);

        jfieldID maxGID = env->GetFieldID( ColorBlockClass, "maxG", "I");
        env->SetIntField( colorBlock, maxGID, colorEdit.colorBlock[i].maxG);

        jfieldID maxBID = env->GetFieldID( ColorBlockClass, "maxB", "I");
        env->SetIntField( colorBlock, maxBID, colorEdit.colorBlock[i].maxB);
    }

}


void GetColorInfo(ColorBlock * colorBlock, unsigned char * color, int width, int height, int bit, int cx, int cy, int sr);
void ScanColor::UpdatBuff(unsigned char * color, int width, int height, int bit ,RecogResult &pRealRes)
{
    // YUV_TO_RGB(color, width, height, rgbBuff, bit);

    // UpdatRGBBuff(color, width, height, bit);

    //计算白板的光照强度
    colorEdit.lightLevel = GetLightLevel(color, width, height, bit);
  LOGE("光强为%d ",colorEdit.lightLevel);

    for(int ii = 0 ; ii < 11 ; ii ++) {

    int top =  pRealRes.realRect[ii].top;
    int left =  pRealRes.realRect[ii].left;
    int right =  pRealRes.realRect[ii].right;
    int bottom =  pRealRes.realRect[ii].bottom;
    int x = ((right - left) / 2) + left;
    int y = ((bottom - top) / 2) + top;
    GetColorInfo(&colorEdit.colorBlock[ii], color, width, height, bit, x, y, 5);
  LOGE("进入 x%d ,y %d",x,y);
  LOGE("进入 width%d ,height %d",width,height);
  LOGE("进入 B%d ,G %d ,R %d" , color[(y*width + x) *3 + 0], color[(y*width + x) *3 + 1], color[(y*width + x) *3 + 2]);

    }


    for(int i = 0; i < 11; i++)
    {
        if(colorEdit.lightLevel > 0)
            colorEdit.colorBlock[i].ratio = colorEdit.colorBlock[i].lightLevel / (float)colorEdit.lightLevel;
        else
            colorEdit.colorBlock[i].ratio = 0;

        int lightID = this->GetIDBYLightLevel(i);
        int MaxRGBID = this->GetIDBYMaxRGB(i);
        colorEdit.colorBlock[i].ID = lightID;
    }

   // UpDataColorEdit();

   // RGB_TO_YUV(rgbBuff, width, height, bit, color);
}



int ScanColor::GetLightLevel(unsigned char * color, int width, int height, int bit)
{
    int lightLevel = 0;

    int size = 0;
    for(int i = 0; i < height; i++)
    {
        int hid = i * width * bit;
        for(int j = 0; j < width; j++)
        {
            int wid = j * bit;

            float H, S, V;
            RGB_HSV(color[hid + wid + 2], color[hid + wid + 1], color[hid + wid + 0], H, S, V);

            colorEdit.colorLevel[(int)V]++;
            if (colorEdit.colorLevel[(int)V] > size)
                size = colorEdit.colorLevel[(int)V];
        }
    }

    for (int i = 0; i < 256; i++)
    {
        int h = colorEdit.colorLevel[i];
        float bl = h / (float)size;
        colorEdit.colorLevel[i] = 255 * bl;
    }

    int maxH = 0;
    for (int i = 0; i < 256; i++)
    {
        int h = colorEdit.colorLevel[i];
        if (h > 255) h = 255;
        int wid = i * bit;
        for (int j = 255 - h; j < 256; j++)
        {
            int hid = j * width * bit;
            if (i > 100 && i < 256)
            {
                //if (wid >= 0 && hid >= 0 && j < height)
                //{
                //    color[hid + wid + 0] = 255;
                //    color[hid + wid + 1] = 255;
                 //   color[hid + wid + 2] = 255;
                //}


                if (h > maxH || h > 20)
                {
                    maxH = h;
                    lightLevel = i;
                }
            }
            else
            {
                //if (wid >= 0 && hid >= 0 && j < height)
                //{
                //    color[hid + wid + 0] = 127;
                //    color[hid + wid + 1] = 127;
                //    color[hid + wid + 2] = 127;
                //}

            }
        }
    }

    //int h = colorEdit.colorLevel[colorEdit.lightLevel];
   // if (h > 255) h = 255;
   // int wid = lightLevel * bit;
    ////if (colorEdit->image && colorEdit->imageData.Width > 0 && colorEdit->imageData.Height > 0)
    //for (int j = 255 - h; j < 256; j++)
   // {
    //    int hid = j * width * bit;

    //     if (wid >= 0 && hid >= 0 && j < height)
    //    {
    //        color[hid + wid + 0] = 0;
    //        color[hid + wid + 1] = 0;
    //        color[hid + wid + 2] = 255;
    //    }
   // }

    return lightLevel;
}

void GetColorInfo(ColorBlock * colorBlock, unsigned char * color, int width, int height, int bit, int cx, int cy, int sr)
{
    for(int i = 0; i < 256; i++)
    {
        colorBlock->colorLevel[i] = 0;
    }

    int size = 0;
    //for(int i = height - 345-10; i < height - 345+10; i++)
    for(int i = cy-sr; i < cy+sr; i++)
    {
        int hid = i * width * bit;
        for(int j = cx - sr; j < cx + sr; j++)
        {
            int wid = j * bit;

            float H, S, V;
            RGB_HSV(color[hid + wid + 2], color[hid + wid + 1], color[hid + wid + 0], H, S, V);

            //    color[hid + wid + 0] = 0;
            //    color[hid + wid + 1] = 0;
             //  color[hid + wid + 2] = 255;

            colorBlock->colorLevel[(int)V]++;
            if (colorBlock->colorLevel[(int)V] > size)
                size = colorBlock->colorLevel[(int)V];
        }
    }

    for (int i = 0; i < 256; i++)
    {
        int h = colorBlock->colorLevel[i];
        float bl = h / (float)size;
        colorBlock->colorLevel[i] = 255 * bl;
    }

    colorBlock->lightLevel = 0;
    colorBlock->minR = 255;
    colorBlock->maxR = 0;
    colorBlock->minG = 255;
    colorBlock->maxG = 0;
    colorBlock->minB = 255;
    colorBlock->maxB = 0;

    int maxH = 0;
    for (int i = 0; i < 256; i++)
    {
        int h = colorBlock->colorLevel[i];
        if (h > 255) h = 255;
        int wid = i * bit + (cx - sr) * bit;
        for (int j = 255 - h; j < 256; j++)
        {
            int hid = j * width * bit + (cy-sr) * width * bit;
            if (i > 80 && i < 256)
            {
                if (h > maxH)
                {
                    maxH = h;
                    colorBlock->lightLevel = i;
                }
//                    if (wid >= 0 && hid >= 0 && j < height)
//                    {
//                        color[hid + wid + 0] = 0;
//                        color[hid + wid + 1] = 0;
//                        color[hid + wid + 2] = 255;
//                    }

            }
            else
            {
//                    if (wid >= 0 && hid >= 0 && j < height)
//                    {
//                        color[hid + wid + 0] = 127;
//                        color[hid + wid + 1] = 127;
//                        color[hid + wid + 2] = 127;
//                    }
            }
        }
    }

    int minR = 255, minG = 255, minB = 255;
    int maxR = 0, maxG = 0, maxB = 0;
    for(int i = cy-sr; i < cy+sr; i++)
    {
        int hid = i * width * bit;
        for(int j = cx - sr; j < cx + sr; j++)
        {
            int wid = j * bit;

            float H, S, V;
            RGB_HSV(color[hid + wid + 2], color[hid + wid + 1], color[hid + wid + 0], H, S, V);

            if(color[hid + wid + 2] < minR) minR = color[hid + wid + 2];
            if(color[hid + wid + 1] < minG) minG = color[hid + wid + 1];
            if(color[hid + wid + 0] < minB) minB = color[hid + wid + 0];

            if(color[hid + wid + 2] > maxR) maxR = color[hid + wid + 2];
            if(color[hid + wid + 1] > maxG) maxG = color[hid + wid + 1];
            if(color[hid + wid + 0] > maxB) maxB = color[hid + wid + 0];
        }
    }

    colorBlock->minR = (minR + maxR) / 2;
    colorBlock->minG = (minG + maxG) / 2;
    colorBlock->minB = (minB + maxB) / 2;
}


//void ScanColor::UpDataBuff(unsigned char * color, int width, int height, int bit, int x, int y, int id)
//{
  //  GetColorInfo(&this->colorEdit.colorBlock[id], rgbBuff, width, height, bit, x, y,5);
//}

void ScanColor::UpdatRGBBuff(unsigned char * color, int width, int height, int bit)
{
    //计算白板的光照强度
    colorEdit.lightLevel = GetLightLevel(rgbBuff, width, height, bit);
    //Rect(345-25, 635 + i * 97 - 25, 345+25, 635 + i * 97 + 25);
    for(int k = 0; k < 11; k++)
    {
        GetColorInfo(&colorEdit.colorBlock[k], rgbBuff, width, height, bit, 635 + k * 97, height-343, 10);
        continue;

        for(int i = 0; i < 256; i++)
        {
            colorEdit.colorBlock[k].colorLevel[i] = 0;
        }

        int size = 0;
        //for(int i = height - 345-10; i < height - 345+10; i++)
        for(int i = height-343-10; i < height-343+10; i++)
        {
            int hid = i * width * bit;
            for(int j = 635 + k * 97 - 10; j < 635 + k * 97 + 10; j++)
            {
                int wid = j * bit;

                float H, S, V;
                RGB_HSV(color[hid + wid + 2], color[hid + wid + 1], color[hid + wid + 0], H, S, V);

//                color[hid + wid + 0] = 0;
//                color[hid + wid + 1] = 0;
//                color[hid + wid + 2] = 0;

                colorEdit.colorBlock[k].colorLevel[(int)V]++;
                if (colorEdit.colorBlock[k].colorLevel[(int)V] > size)
                    size = colorEdit.colorBlock[k].colorLevel[(int)V];

            }
        }

        for (int i = 0; i < 256; i++)
        {
            int h = colorEdit.colorBlock[k].colorLevel[i];
            float bl = h / (float)size;
            colorEdit.colorBlock[k].colorLevel[i] = 255 * bl;
        }
        //LOGI("-- %d,  ", k);
        colorEdit.colorBlock[k].lightLevel = 0;
        colorEdit.colorBlock[k].minR = 255;
        colorEdit.colorBlock[k].maxR = 0;
        colorEdit.colorBlock[k].minG = 255;
        colorEdit.colorBlock[k].maxG = 0;
        colorEdit.colorBlock[k].minB = 255;
        colorEdit.colorBlock[k].maxB = 0;

        int maxH = 0;
        for (int i = 0; i < 256; i++)
        {
            int h = colorEdit.colorBlock[k].colorLevel[i];
            if (h > 255) h = 255;
            int wid = i * bit + (635 + k * 97 - 10) * bit;
            for (int j = 255 - h; j < 256; j++)
            {
                int hid = j * width * bit + (735-10) * width * bit;
                if (i > 80 && i < 256)
                {


                    if (h > maxH)
                    {
                        maxH = h;
                        colorEdit.colorBlock[k].lightLevel = i;
                    }
//                    if (wid >= 0 && hid >= 0 && j < height)
//                    {
//                        color[hid + wid + 0] = 0;
//                        color[hid + wid + 1] = 0;
//                        color[hid + wid + 2] = 255;
//                    }

                }
                else
                {
//                    if (wid >= 0 && hid >= 0 && j < height)
//                    {
//                        color[hid + wid + 0] = 127;
//                        color[hid + wid + 1] = 127;
//                        color[hid + wid + 2] = 127;
//                    }
                }
            }
        }


        int minR = 255, minG = 255, minB = 255;
        int maxR = 0, maxG = 0, maxB = 0;
        for(int i = 735-10; i < 735+10; i++)
        {
            int hid = i * width * bit;
            for(int j = 635 + k * 97 - 10; j < 635 + k * 97 + 10; j++)
            {
                int wid = j * bit;

                float H, S, V;
                RGB_HSV(color[hid + wid + 2], color[hid + wid + 1], color[hid + wid + 0], H, S, V);

                if(color[hid + wid + 2] < minR) minR = color[hid + wid + 2];
                if(color[hid + wid + 1] < minG) minG = color[hid + wid + 1];
                if(color[hid + wid + 0] < minB) minB = color[hid + wid + 0];

                if(color[hid + wid + 2] > maxR) maxR = color[hid + wid + 2];
                if(color[hid + wid + 1] > maxG) maxG = color[hid + wid + 1];
                if(color[hid + wid + 0] > maxB) maxB = color[hid + wid + 0];
            }
        }

        colorEdit.colorBlock[k].minR = (minR + maxR) / 2;
        colorEdit.colorBlock[k].minG = (minG + maxG) / 2;
        colorEdit.colorBlock[k].minB = (minB + maxB) / 2;

        float maxColorV = 0;
        int vR = 0, vG = 0, vB = 0;
        for(int i = 735-10; i < 735+10; i++)
        {
            int hid = i * width * bit;
            for(int j = 635 + k * 97 - 10; j < 635 + k * 97 + 10; j++)
            {
                int wid = j * bit;

                float H, S, V;
                RGB_HSV(color[hid + wid + 2], color[hid + wid + 1], color[hid + wid + 0], H, S, V);

                int maxRGB = Max(Max(color[hid + wid + 2], color[hid + wid + 1]), color[hid + wid + 0]);
                int minRGB = Min(Min(color[hid + wid + 2], color[hid + wid + 1]), color[hid + wid + 0]);

                float maxcv = (maxRGB - minRGB) / V;
                if(maxcv > maxColorV)
                {
                    maxColorV = maxcv;

                    colorEdit.colorBlock[k].maxR = color[hid + wid + 2];
                    colorEdit.colorBlock[k].maxG = color[hid + wid + 1];
                    colorEdit.colorBlock[k].maxB = color[hid + wid + 0];
                }

            }
        }

        //去除光强度保留颜色本值
        int maxRGB = Max(Max(colorEdit.colorBlock[k].maxR, colorEdit.colorBlock[k].maxG), colorEdit.colorBlock[k].maxB);
        int minRGB = Min(Min(colorEdit.colorBlock[k].maxR, colorEdit.colorBlock[k].maxG), colorEdit.colorBlock[k].maxB);

        float colorBL = 255 / (float)maxRGB;

        colorEdit.colorBlock[k].maxR *= colorBL;
        colorEdit.colorBlock[k].maxG *= colorBL;
        colorEdit.colorBlock[k].maxB *= colorBL;

        //LOGI("colorEdit.colorBlock[k].maxR %d,  ", colorEdit.colorBlock[k].maxR);

    }



}

void ScanColor::YUV_TO_RGB(unsigned char * yuvColor, int width, int height, unsigned char * rgbColor, int bit)
{
    int offset = width * height;
    // int offset2 = offset + offset / 4;
    for(int i = 0; i < height; i++)
    {
        int hid1 = i * width;
        int hid2 = i / 2 * width;
        for(int j = 0; j < width; j++)
        {
            int wid1 = j;
            int wid2 = j;

            unsigned char *Y = &yuvColor[hid1 + wid1];
            unsigned char *U = 0;
            unsigned char *V = 0;

            int uvid = j % 2 > 0.0 ? 1 : 0;

            if (uvid == 0) {
                V = &yuvColor[offset + hid2 + wid2 + 0];
                U = &yuvColor[offset + hid2 + wid2 + 1];
            }
            else {
                V = &yuvColor[offset + hid2 + wid2 - 1];
                U = &yuvColor[offset + hid2 + wid2 + 0];
            }

            unsigned char *B = &rgbColor[i * width * 3 + j * 3 + 0];
            unsigned char *G = &rgbColor[i * width * 3 + j * 3 + 1];
            unsigned char *R = &rgbColor[i * width * 3 + j * 3 + 2];

            YUV_RGB(*Y, *U, *V, *B, *G, *R);
        }
    }
}

void ScanColor::RGB_TO_YUV(unsigned char * rgbColor, int width, int height, int bit, unsigned char * yuvColor)
{
    int offset = width * height;

    for(int i = 0; i < height; i++)
    {
        int hid1 = i * width;
        int hid2 = i / 2 * width;
        for (int j = 0; j < width; j++)
        {
            int wid1 = j;
            int wid2 = j;

            unsigned char *Y = &yuvColor[hid1 + wid1];
            unsigned char *U = 0;
            unsigned char *V = 0;

            int uvid = j % 2 > 0.0 ? 1 : 0;

            if (uvid == 0) {
                V = &yuvColor[offset + hid2 + wid2 + 0];
                U = &yuvColor[offset + hid2 + wid2 + 1];
            }
            else {
                V = &yuvColor[offset + hid2 + wid2 - 1];
                U = &yuvColor[offset + hid2 + wid2 + 0];
            }

            unsigned char B = rgbColor[i * width * 3 + j * 3 + 0];
            unsigned char G = rgbColor[i * width * 3 + j * 3 + 1];
            unsigned char R = rgbColor[i * width * 3 + j * 3 + 2];

            RGB_YUV(R, G, B, *Y, *U, *V);
        }
    }
}

void ScanColor::YUV_RGB(unsigned char Y, unsigned char CR, unsigned char CB, unsigned char &R, unsigned char &G, unsigned char&B)
{
    int r = Y + 1.402 * (CR-128);
    int g = Y - 0.34414 * (CB-128) - 0.71414 * (CR-128);
    int b = Y + 1.772 * (CB-128);

    R = r>255? 255 : r<0 ? 0 : r;
    G = g>255? 255 : g<0 ? 0 : g;
    B = b>255? 255 : b<0 ? 0 : b;
}

void ScanColor::RGB_YUV(unsigned char R, unsigned char G, unsigned char B, unsigned char &Y, unsigned char &UR, unsigned char &VB)
{
    Y = 0.299 * R + 0.587 * G + 0.114 * B;
    UR = - 0.1678 * R - 0.3313 * G + 0.5 * B + 128;
    VB = 0.5 * R - 0.4187 * G - 0.0813 * B + 128;
}

int ScanColor::GetIDBYLightLevel(int id)
{
    int vID = 0;

    switch(id)
    {
        case 0://白细胞
        {
            vID = 1;

            if(colorEdit.colorBlock[id].ratio < 0.8)
                vID = 2;

            if(colorEdit.colorBlock[id].ratio < 0.67)
                vID = 3;

            if(colorEdit.colorBlock[id].ratio < 0.5)
                vID = 4;
        }
            break;
        case 1://亚硝酸盐
        {
            vID = 1;

            int max = Max(Max(colorEdit.colorBlock[id].minR, colorEdit.colorBlock[id].minG), colorEdit.colorBlock[id].minB);
            int min = Min(Min(colorEdit.colorBlock[id].minR, colorEdit.colorBlock[id].minG), colorEdit.colorBlock[id].minB);

            float v = 0;
            if(colorEdit.lightLevel > 0)
                v = (max - min) / (float)max;

            //LOGI("--------------------%f  ", v);

            if(v < 0.3)
                vID = 1;
            else
                vID = 2;
        }
            break;
        case 2://尿胆原
        {



        }
            break;
        case 3://蛋白质
        {
            vID = 1;

            //微  R: minR:160.00, maxR:163.00            G: minG:161.00, maxG:166.00            B: minB:91.00, maxB:95.00
            //0.3 R: minR:147.00, maxR:147.00            G: minG:157.00, maxG:159.00            B: minB:88.00, maxB:90.00
            //1   R: minR:115.00, maxR:119.00            G: minG:153.00, maxG:157.00            B: minB:116.00, maxB:118.00
            //3   R: minR:80.00, maxR:85.00            G: minG:133.00, maxG:139.00            B: minB:107.00, maxB:115.00
            //20  R: minR:53.00, maxR:62.00            G: minG:100.00, maxG:109.00            B: minB:82.00, maxB:92.00

            int maxRG = Max(colorEdit.colorBlock[id].minR, colorEdit.colorBlock[id].minG);
            int minRG = Min(colorEdit.colorBlock[id].minR, colorEdit.colorBlock[id].minG);

            int maxRGB = Max(Max(colorEdit.colorBlock[id].minR, colorEdit.colorBlock[id].minG), colorEdit.colorBlock[id].minB);
            int minRGB = Min(Min(colorEdit.colorBlock[id].minR, colorEdit.colorBlock[id].minG), colorEdit.colorBlock[id].minB);

            float lightV = colorEdit.lightLevel / 255.0;

            if(colorEdit.colorBlock[id].minR / maxRGB  == 1)
            {
                //float bl = colorEdit.colorBlock[id].minG / (float)maxRGB;
                //LOGI("R %f  ", bl);
            }
            //y主色调为绿色，辅色为红色，红色逐渐减少影响绿色，呈现从黄到绿的变化
            if(colorEdit.colorBlock[id].minG / maxRGB  == 1)
            {
                float bl = colorEdit.colorBlock[id].minR / (float)maxRGB;
                // LOGI("G %f  ", bl);

                if(bl < 0.94)
                    vID = 2;

                if(bl < 0.9)
                    vID = 3;

                if(bl < 0.85)
                    vID = 4;

                if(bl < 0.8)
                    vID = 5;
            }
        }
            break;
        case 4://酸咸度
        {
            vID = 1;
            //5  R: minR:168.00, maxR:186.00            G: minG:96.00, maxG:108.00            B: minB:22.00, maxB:33.00
            //6 R: minR:178.00, maxR:182.00            G: minG:113.00, maxG:114.00            B: minB:21.00, maxB:26.00
            //6.5 R: minR:179.00, maxR:183.00            G: minG:132.00, maxG:134.00            B: minB:18.00, maxB:24.00
            //7   R: minR:143.00, maxR:153.00            G: minG:124.00, maxG:131.00            B: minB:22.00, maxB:26.00
            //7.5 R: minR:117.00, maxR:127.00            G: minG:121.00, maxG:132.00            B: minB:27.00, maxB:52.00
            //8   R: minR:71.00, maxR:84.00            G: minG:96.00, maxG:104.00            B: minB:41.00, maxB:47.00
            //8.5 R: minR:51.00, maxR:60.00            G: minG:89.00, maxG:99.00            B: minB:40.00, maxB:51.00
            //LOGI("酸咸度 %f  ", colorEdit.colorBlock[id].ratio );
            if(colorEdit.colorBlock[id].ratio <= 1.01)
                vID = 2;

            if(colorEdit.colorBlock[id].ratio <= 0.95)
                vID = 3;

            if(colorEdit.colorBlock[id].ratio <= 0.8)
                vID = 4;

            if(colorEdit.colorBlock[id].ratio <= 0.7)
                vID = 5;

            if(colorEdit.colorBlock[id].ratio <= 0.5)
                vID = 6;
        }
            break;
        case 5://潜血
        {
            vID = 1;

            if(colorEdit.colorBlock[id].ratio <= 0.9)
                vID = 2;

            if(colorEdit.colorBlock[id].ratio <= 0.85)
                vID = 3;

            if(colorEdit.colorBlock[id].ratio <= 0.6)
                vID = 4;

            if(colorEdit.colorBlock[id].ratio <= 0.5)
                vID = 5;
        }
            break;
        case 6://比重
        {
            vID = 1;

            if(colorEdit.colorBlock[id].ratio > 0.4)
                vID = 2;

            if(colorEdit.colorBlock[id].ratio > 0.5)
                vID = 3;

            if(colorEdit.colorBlock[id].ratio > 0.55)
                vID = 4;

            if(colorEdit.colorBlock[id].ratio > 0.6)
                vID = 5;

            if(colorEdit.colorBlock[id].ratio > 0.65)
                vID = 6;
        }
            break;
        case 7://酮体
        {
            vID = 1;

            if(colorEdit.colorBlock[id].ratio < 0.95)
                vID = 2;

            if(colorEdit.colorBlock[id].ratio < 0.94)
                vID = 3;

            if(colorEdit.colorBlock[id].ratio < 0.8)
                vID = 4;

            if(colorEdit.colorBlock[id].ratio < 0.6)
                vID = 5;
        }
            break;
        case 8://胆红素
        {

        }
            break;
        case 9://葡萄糖
        {
            vID = 1;

            if(colorEdit.colorBlock[id].ratio < 0.83)
                vID = 2;

            if(colorEdit.colorBlock[id].ratio < 0.75)
                vID = 3;

            if(colorEdit.colorBlock[id].ratio < 0.7)
                vID = 4;

            if(colorEdit.colorBlock[id].ratio < 0.65)
                vID = 5;
        }
            break;
        case 10://抗坏血酸
        {
            vID = 1;

            if(colorEdit.colorBlock[id].ratio > 0.42)
                vID = 2;

            if(colorEdit.colorBlock[id].ratio > 0.9)
                vID = 3;
        }
            break;
    }

    return vID;
}

int ScanColor::GetIDBYMaxRGB(int id)
{
    int vID = 0;

    switch(id)
    {
        case 0://白细胞
        {
            //LOGI("colorEdit.colorBlock[k].maxR %d, %d, %d,  ", colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                vID = 1;
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxB == min)
                {
                    float bl = min / 255.5;

                    int G = colorEdit.colorBlock[id].maxG * bl;

                    bl = G / 255.5;
                    //LOGI("%f  ", bl);

                    vID = 1;
                }
                if(colorEdit.colorBlock[id].maxG == min)
                {
                    float bl = min / 255.5;
                    int B = colorEdit.colorBlock[id].maxB * bl;
                    bl = B / 255.5;

                    if(bl < 0.9)
                        vID = 2;

                    if(bl < 0.5)
                        vID = 3;

                    if(bl < 0.41)
                        vID = 4;

                   // LOGI("maxG %f  ", bl);
                }
            }
//            if(colorEdit.colorBlock[id].maxB == 255)
//            {
//                int min = Min(colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxG);
//                if(colorEdit.colorBlock[id].maxR == min)
//                {
//                    float bl = min / 255.5;
//                    int G = colorEdit.colorBlock[id].maxG * bl;
//                    bl = G / 255.5;
//                    vID = 4;
//                    LOGI("maxB %f  ", bl);
//                }
//            }
        }
            break;
        case 1://亚硝酸盐
        {
            vID = 1;
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                //LOGI("maxR %d  ", colorEdit.colorBlock[id].maxR);
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxG > min)
                {
                    vID = 1;
                }
                if(colorEdit.colorBlock[id].maxB > min)
                {
//                    float bl = min / 255.5;
//                    float V = colorEdit.colorBlock[id].maxB * bl;
                    //LOGI("colorEdit.colorBlock[id].maxB * bl %f  ", V);
                    vID = 2;
                }
            }
        }
            break;
        case 2://尿胆原
        {
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxG > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxG * bl;
                    //LOGI("colorEdit.colorBlock[%d].maxG * bl %f  ", id, V);
                    if(V > 90)
                        vID = 1;
                    if(V <= 90)
                        vID = 2;
                }
                if(colorEdit.colorBlock[id].maxB > min)
                {
                    vID = 3;

                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxB * bl;
                    //LOGI("colorEdit.colorBlock[%d].maxB * bl %f  ", id, V);

                    if(V < 55)
                        vID = 4;
                }
            }
        }
            break;
        case 3://蛋白质
        {
            vID = 1;
            if(colorEdit.colorBlock[id].maxG == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxR > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxB * bl;
                    //LOGI("蛋白质 colorEdit.colorBlock[%d].maxR * bl %f  ", id, V);
                    if(V > 85)
                        vID = 2;
                    if(V > 130)
                        vID = 3;
                }
                if(colorEdit.colorBlock[id].maxB > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxB * bl;
                    //LOGI("蛋白质 colorEdit.colorBlock[%d].maxG * bl %f  ", id, V);

//                    if(V > 85)
//                        vID = 2;
//                    if(V > 130)
//                        vID = 3;
                    if(V > 180)
                        vID = 4;
                    if(V > 185)
                        vID = 5;
                }

            }
        }
            break;
        case 4://酸咸度
        {
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                vID = 1;
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxG > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxG * bl;
                    //LOGI("酸咸度 colorEdit.colorBlock[%d].maxG * bl %f  ", id, V);
                    if(V > 90)
                        vID = 2;
                    if(V > 93)
                        vID = 3;
                }
            }
            if(colorEdit.colorBlock[id].maxG == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxR > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxR * bl;
                    //LOGI("酸咸度 colorEdit.colorBlock[%d].maxB * bl %f  ", id, V);

                    if(V > 60)
                        vID = 4;
                    if(V > 135)
                        vID = 5;
                }
            }
            if(colorEdit.colorBlock[id].maxB == 255)
                vID = 6;
        }
            break;
        case 5://潜血
        {//LOGI("潜血 %d %d %d  ", colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxG > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxG * bl;
                    //LOGI("潜血 %d %f  ", id, V);
                    if(V > 70)
                        vID = 1;

                    if(V > 100)
                        vID = 2;
                }
            }
            if(colorEdit.colorBlock[id].maxG == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxR > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxR * bl;
                    //LOGI("潜血 %d %f  ", id, V);

                    if(V > 100)
                        vID = 3;

                    if(V > 130)
                        vID = 4;

                    if(V > 170)
                        vID = 5;
                }
            }
        }
            break;
        case 6://比重
        {
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxG > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxG * bl;
                    //LOGI("潜血 %d %f  ", id, V);
                    if(V <190)
                        vID = 3;

                    if(V < 160)
                        vID = 4;

                    if(V < 140)
                        vID = 5;

                    if(V < 120)
                        vID = 6;
                }
            }

            if(colorEdit.colorBlock[id].maxG == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxR > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxR * bl;
                    //LOGI("比重 %d %f  ", id, V);

                    //if(V < 170)
                        vID = 1;

                    if(V < 180 )
                        vID = 2;
                }
            }
        }
            break;
        case 7://酮体
        {
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxG > min)
                {
                    vID = 1;
                }
                if(colorEdit.colorBlock[id].maxB > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxB * bl;

                    //LOGI("酮体 %d %f  ", id, V);

                    //if(V < 180 )
                        vID = 2;

                    if(V < 53 )
                        vID = 3;
                }
            }
        }
            break;
        case 8://胆红素
        {
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxG > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxG * bl;

                    //LOGI("胆红素 %d %f  ", id, V);

                    if(V <= 160 )
                        vID = 1;

                    if(V > 160 )
                        vID = 2;

                    if(V <= 140 )
                        vID = 3;

                }

            }
        }
            break;
        case 9://葡萄糖
        {
            if(colorEdit.colorBlock[id].maxG == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxR > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxR * bl;

                    //LOGI("葡萄糖 %d %f  ", id, V);

                    if(V > 110 )
                        vID = 1;

                    if(V < 110 )
                        vID = 2;

                }
            }
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxG > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxG * bl;

                    //LOGI("葡萄糖 %d %f  ", id, V);

                    vID = 3;

                    if(V < 80 )
                        vID = 4;

                    if(V < 50 )
                        vID = 5;
                }
            }
        }
            break;
        case 10://抗坏血酸
        {
            if(colorEdit.colorBlock[id].maxB == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxG, colorEdit.colorBlock[id].maxR);
                if(colorEdit.colorBlock[id].maxB > min)
                {
                    vID = 1;
                }
            }
            if(colorEdit.colorBlock[id].maxG == 255)
            {
                int min = Min(colorEdit.colorBlock[id].maxR, colorEdit.colorBlock[id].maxB);
                if(colorEdit.colorBlock[id].maxR > min)
                {
                    float bl = min / 255.0;
                    float V = colorEdit.colorBlock[id].maxR * bl;
                    LOGI("葡萄糖 %d %f  ", id, V);
                }
                vID = 2;
            }
            if(colorEdit.colorBlock[id].maxR == 255)
            {
                vID = 3;
            }
        }
            break;
    }

    return vID;

}